from rest_framework.views import APIView
from django.http import JsonResponse
from api.utils.auth import Authticatition
from api.utils.permission import MyPermission
from api.utils.throttle import VisitThrottle, MyThrottle
from api.order.models import Order
from crm.http_config import API_TEMPLATE, HttpCode
from rest_framework.versioning import QueryParameterVersioning, URLPathVersioning


class AuthView(APIView):
    """匿名用户"""

    def post(self, request, *args, **kwargs):
        res = API_TEMPLATE.copy()
        print(request.version)
        return JsonResponse(res)


class OrderView(APIView):

    authentication_classes = [Authticatition, ]  # 登陆状态 局部用全局不同
    permission_classes = [MyPermission, ]  # 权限 局部用全局不同
    throttle_classes = [VisitThrottle, ]  # 节流
    versioning_class = QueryParameterVersioning  # url传参获取
    # versioning_class = URLPathVersioning  # url传参获取

    def get(self, request, *args, **kwargs):
        version = request.version
        version_obj = request.versioning_scheme
        print(version_obj,version)
        res = API_TEMPLATE.copy()
        data = Order.objects.all().values()
        res.update(code=HttpCode.ok.value[0], msg=HttpCode.ok.value[1], data=str(data))
        return JsonResponse(res)
